﻿using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.ObjectModel;
using GalaSoft.MvvmLight.Command;
using db;
using SGTv2.Model;
using dominio;
using System.Collections.Generic;

namespace SGTv2.ViewModel
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class RemitoViewModel : ViewModelBase, IObserver<object>
    {
        /// <summary>
        /// Initializes a new instance of the RemitoViewModel class.
        /// </summary>
        /// 
        private Sector _SectorEnUso;
        private Deposito _DepositoEnUso;
        private DateTime _Fecha;
        private string _RemitoExterno;
        private Proveedor _ProveedorMercaderia;
        private float _PesoReal;
        private List<Proveedor> _Proveedores;
        private ObservableCollection<Remito> _Remitos;
        private string _Notas;
        private Remito _RemitoEnPantalla;
        private bool _tipoEntrada = true;
        private bool _tipoSalida = false;
        public RelayCommand InsertarRemito { get; private set; }
        public RelayCommand Imprimir { get; private set; }
        



        Messenger mes = Messenger.Default;


        #region Fields
        public bool TipoEntrada
        {
            get { return _tipoEntrada; }
            set
            {
                _tipoEntrada = value;
                _tipoSalida = !value;
                RaisePropertyChanged("TipoEntrada");
                RaisePropertyChanged("TipoSalida");
            }
        }

        public bool TipoSalida
        {
            get { return _tipoSalida; }
            set
            {
                _tipoSalida = value;
                _tipoEntrada = !value;
                RaisePropertyChanged("TipoSalida");
                RaisePropertyChanged("TipoEntrada");
            }
        }



        public Sector SectorEnUso
        {
            get { return _SectorEnUso; }
            set
            {
                _SectorEnUso = value;
                RaisePropertyChanged("SectorEnUso");
            }
        }

        public Deposito DepositoEnUso
        {
            get { return _DepositoEnUso; }
            set
            {
                _DepositoEnUso = value;
                RaisePropertyChanged("DepositoEnUso");
            }
        }


        public DateTime Fecha
        {
            get { return _Fecha; }
            set
            {
                _Fecha = value;
                RaisePropertyChanged("Fecha");
            }
        }
        public string RemitoExterno
        {
            get { return _RemitoExterno; }
            set
            {
                _RemitoExterno = value;
                RaisePropertyChanged("RemitoExterno");
            }
        }

        public Proveedor ProveedorMercaderia
        {
            get { return _ProveedorMercaderia; }
            set
            {
                _ProveedorMercaderia = value;
                RaisePropertyChanged("ProveedorMercaderia");
            }
        }

        public List<Proveedor> Proveedores
        {
            get { return _Proveedores; }
            set
            {
                _Proveedores = value;
                RaisePropertyChanged("Proveedores");
            }
        }

        public float PesoReal
        {
            get { return _PesoReal; }
            set
            {
                _PesoReal = value;
                RaisePropertyChanged("PesoReal");
            }
        }

        public string Notas
        {
            get { return _Notas; }
            set
            {
                _Notas = value;
                RaisePropertyChanged("Notas");
            }
        }
        public ObservableCollection<Remito> Remitos
        {
            get { return _Remitos; }
            set
            {
                _Remitos = value;
                RaisePropertyChanged("Remitos");
            }
        }
        public Remito RemitoEnPantalla
        {
            get { return _RemitoEnPantalla; }
            set
            {
                _RemitoEnPantalla = value;
                RaisePropertyChanged("RemitoEnPantalla");
            }
        }
        #endregion Fields




        public RemitoViewModel()
        {
            if (IsInDesignMode)
            {
            ////    // Code runs in Blend --> create design time data.
            }
            else
            {
               
              
                inicializarFormulario();
                  Sistema.getInstance().Subscribe(this);
                InsertarRemito = new RelayCommand(GuardarRemito);
              
        
            }
        }

        




        private void inicializarFormulario()
        {

            DepositoEnUso = Sistema.getInstance().GetDepositoActual();
            RemitoEnPantalla = new Remito();
            Fecha = DateTime.Now;
       
            Proveedores = Sistema.getInstance().GetProveedores();
            RemitoEnPantalla.Fecha = Fecha;
          
        
            
            
        }

      

        


        private void GuardarRemito()
        {
            RemitoEnPantalla.Sector = SectorEnUso;
            int tipo = -1;
            if (_tipoEntrada)
                tipo = 1;
            Sistema.getInstance().InsertarRemito(RemitoEnPantalla,tipo);
            RemitoEnPantalla = new Remito();
            RemitoEnPantalla.Fecha = Fecha;
        }


          public virtual void OnCompleted()
        {

        }
        public virtual void OnError(Exception e)
        {
        }
        public virtual void OnNext(Object value)
        {
            inicializarFormulario();
        }

      
  

        public override void Cleanup()
        {
        ////    // Clean own resources if needed

            base.Cleanup();
        }
    }
}